//
//  SetController.swift
//  JianTingSwift
//
//  Created by zhibao on 2024/11/7.
//

import UIKit

class SetController: SecSuperController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.bgImgView.isHidden = false
        self.gk_navTitle = "设置"
        self.initView()
    }
    func initView(){
        var titles = [
            [
                "title":"更换手机号","detail":Helper.hidefourNum(fromPhone: UserInfoManager.shared().phone)
            ],
            [
                "title":"注销账号","detail":"账号将永久注销,无法恢复"
            ]
        ]
        if UserInfoManager.shared().phone.count < 1 {
            titles = [
                [
                    "title":"绑定手机号","detail":Helper.hidefourNum(fromPhone: UserInfoManager.shared().phone)
                ],
                [
                    "title":"注销账号","detail":"账号将永久注销,无法恢复"
                ]
                ]
        }
        
        for i  in 0 ..< titles.count {
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: pixw(p:15), y: navigationFullHeight() + pixw(p:CGFloat(i * 50)), width: SCREEN_WIDTH - pixw(p:30), height: pixw(p:50))
            btn.tag = 1000 + i
            self.view.addSubview(btn)
            
            let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: pixw(p: 200), height: btn.height))
            titleLabel.text = titles[i]["title"]
            titleLabel.font = MY_SCALE_RegularFONT(13)
            titleLabel.textColor = .white
            btn.addSubview(titleLabel)
            
            btn.addTarget(self, action: #selector(btnCLick(sender:)), for: .touchUpInside)
            
            let imgView = UIImageView(image: UIImage(named: "com_right"))
            btn.addSubview(imgView)
            imgView.snp.makeConstraints { make in
                make.right.centerY.equalTo(btn)
            }
            
            let detailLabel = UILabel()
            detailLabel.text = titles[i]["detail"]
            detailLabel.font = MY_SCALE_RegularFONT(13)
            detailLabel.numberOfLines = 2
            detailLabel.lineBreakMode = .byWordWrapping
            detailLabel.textColor = .white
            btn.addSubview(detailLabel)
            detailLabel.snp.makeConstraints { make in
                make.left.equalTo(titleLabel.snp.right).offset(pixw(p:8));
                make.right.equalTo(imgView.snp.left).offset(pixw(p:-8));
                make.centerY.equalTo(btn);
            }
        }
        
        let outBtn = UIButton(type: .custom)
        outBtn.layer.cornerRadius = 25
        outBtn.backgroundColor  = MYColor.color(withHexString: "25282D")
        self.view.addSubview(outBtn)
        outBtn.setTitle("退出登录", for: .normal)
        outBtn.setTitleColor(.white, for: .normal)
        outBtn.titleLabel?.font = MY_SCALE_RegularFONT(15)
        outBtn.addTarget(self, action: #selector(outClick), for: .touchUpInside)
        outBtn.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15));
            make.right.equalTo(self.view).offset(pixw(p:-15));
            make.height.equalTo(pixw(p:55));
            make.bottom.equalTo(self.view.snp.bottom).offset(pixw(p:-20) - safeDistanceBottom());
        }
        
    }
    @objc func btnCLick(sender:UIButton){
        if sender.tag == 1000 {
            let controller = ChangePhoneController()
            controller.type = 1
            if UserInfoManager.shared().phone.count < 1 {
                controller.type = 2
            }
            self.navigationController?.pushViewController(controller, animated: true)
            
        }else if sender.tag == 1001 {
            let controller = DelePhoneController()
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }
    
    @objc func outClick(){
        QMUITips.showSucceed("退出成功")
        UserInfoManager.shared().initEmptyData()
        self.navigationController?.popToRootViewController(animated: true)
    }

}
